package com.example.sys.org.service;

import com.example.core.mapping.BaseMapping;
import com.example.core.service.BaseServiceImpl;
import com.example.sys.org.dao.SysRoleDao;
import com.example.sys.org.dao.SysUserRoleDao;
import com.example.sys.org.mapping.SysUserRoleMapping;
import com.example.sys.org.po.SysRole;
import com.example.sys.org.po.SysUserRole;
import com.example.sys.org.vo.SysUserRoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRoleDao, SysUserRole, SysUserRoleVo> implements SysUserRoleService {

    @Resource
    private SysRoleDao sysRoleDao;

    @Override
    public Class<? extends BaseMapping> getMappingClass() {
        return SysUserRoleMapping.class;
    }

    @Override
    public boolean hasRole(String userId, String roleCode) {
        return existOpenRole(roleCode) || getBaseMapper().hasRole(userId, roleCode).size() > 0;
    }

    @Override
    public boolean existOpenRole(String roleCode) {
        SysRole sysRole = sysRoleDao.getRoleByCode(roleCode);
        return sysRole == null ? false : sysRole.isOpened();
    }
}
